Imports System.Transactions

''' -----------------------------------------------------------------------------
''' Project	 : BizBlox
''' Class	 : BizBlox.DOTransaction
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Provide simple transaction capabilities to DataObjects.
''' 
''' </summary>
''' <remarks>
''' Note that Transactions will only work inside the context of multiple writes, 
''' Since the batch of SQL is written to the database using the transaction model
''' specified at COMMIT. 
''' 
''' This essentially means that this cannot support true 
''' ReadUnCommitted or Chaos modes since the data is not visible until the transaction
''' is actually BEING committed. This is why transactions are not supported for DataObject 
''' read methods.
''' transaction
''' </remarks>
''' <history>
''' 	[cinj]	21/03/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class DOTransaction
    Private m_sql As ArrayList
    Private m_transaction As IDbTransaction
    Private isolationSet As Boolean = False

    <FlagsAttribute()> Public Enum IsolationLevel
        Chaos = 16
        ReadCommitted = 4096
        ReadUncommitted = 256
        RepeatableRead = 65535
        Serializable = 1048576
        Unspecified = -1
    End Enum

    ''' <summary>
    ''' Create an empty transaction
    ''' </summary>
    ''' <param name="isolation">Use the DOTransaction Isolation Level which maps to a real Data.IsolationLevel enum</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal isolation As Integer)
        m_sql = New ArrayList
        Dim DAObj As New DataAccess
        Me.m_transaction = DAObj.BeginTransaction(isolation)
        isolationSet = True
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Create an empty transaction.
    ''' </summary>
    ''' <remarks>
    ''' Legacy code for handling old transactions
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	26/08/2004	Created
    '''     [thoc]  09/03/2007  Updated
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub New()
        m_sql = New ArrayList
    End Sub

    Default Friend ReadOnly Property SQL(ByVal i As Integer) As String
        Get
            Return CStr(m_sql(i))
        End Get
    End Property

    Friend ReadOnly Property count() As Integer
        Get
            Return m_sql.Count
        End Get
    End Property


    Friend Sub AddSQL(ByVal sqlStr As String)
        If isolationSet Then
            Dim DAObj As New DataAccess
            DAObj.AddSQLToTransaction(sqlStr, Me.m_transaction, Me.m_transaction.Connection)
        Else
            m_sql.Add(sqlStr)
        End If
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Commit the transaction
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	26/08/2004	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub Commit()
        If isolationSet Then
            Using scope As New TransactionScope()
                Dim DAObj As New DataAccess
                DAObj.CommitTransaction(Me.m_transaction)
            End Using
        Else
            Dim DAObj As New DataAccess
            DAObj.RunSQLTransaction(Me.m_sql)
        End If
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Commit the transaction
    ''' </summary>
    ''' <param name="il">Use the DOTransaction Isolation Level which maps to a real Data.IsolationLevel enum</param>
    ''' <remarks>
    ''' Legacy code for handling old transactions
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	26/08/2004	Created
    '''     [thoc]  09/04/2007  Updated 
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub Commit(ByVal il As IsolationLevel)
        Dim DAObj As New DataAccess
        DAObj.RunSQLTransaction(Me.m_sql, il)
    End Sub
End Class
